//
//  TabbarItemPopoverView.m
//  测试111
//
//  Created by Burn on 2018/10/18.
//  Copyright © 2018年 buren. All rights reserved.
//

#import "TabbarItemPopoverView.h"
#import<UIKit/UIKit.h>




@interface TabbarItemPopoverView ()

/*TopButton*/
@property(strong,nonatomic) UIButton *topBTN;
/*BottomBTN*/
@property(strong,nonatomic) UIButton *bottomBTN;

@end



@implementation TabbarItemPopoverView

- (instancetype)initWithFrame:(CGRect)frame andTitle:(NSString *)topTitle andBottomTitle:(NSString *)bottmTitle
{
    self = [super initWithFrame:frame];
    if (self) {
        self.topBTN = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.topBTN setTitle:topTitle forState:UIControlStateNormal];
        self.topBTN.frame = CGRectMake(0, 0, frame.size.width, frame.size.height / 2);
        [self.topBTN addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];
        self.topBTN.tag = 2001;
        self.topBTN.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.topBTN];
        
        self.bottomBTN = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.bottomBTN setTitle:bottmTitle forState:UIControlStateNormal];
        self.bottomBTN.frame = CGRectMake(0, frame.size.height / 2, frame.size.width, frame.size.height / 2);
        [self.bottomBTN addTarget:self action:@selector(selectButton:) forControlEvents:UIControlEventTouchUpInside];
        self.bottomBTN.tag = 2002;
        self.bottomBTN.titleLabel.font = [UIFont systemFontOfSize:12];
        [self addSubview:self.bottomBTN];
        self.backgroundColor = [UIColor clearColor];
        
        UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(0, (frame.size.height - 3) / 2 - .25, frame.size.width, .5)];
        line.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:line];
    }
    return self;
}

- (void)drawRect:(CGRect)rect{
    [super drawRect:rect];
    
    CGRect roundRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height - 8);
    
    CGPoint s = CGPointMake(rect.size.width / 2 - 7, roundRect.size.height + roundRect.origin.y);
    CGPoint o = CGPointMake(rect.size.width / 2, rect.size.height + rect.origin.y);
    CGPoint e = CGPointMake(rect.size.width / 2 + 7, roundRect.size.height + roundRect.origin.y);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:roundRect byRoundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(6, 6)];
    [path moveToPoint:s];
    [path addLineToPoint:o];
    [path addLineToPoint:e];
    [path closePath];
    [[UIColor lightGrayColor] setFill];
    [path fill];
    
}

- (void)animation{
    CAKeyframeAnimation *scaleAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    NSValue *value1 = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    NSValue *value2 = [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.075, 1.075, 1)];
    NSValue *value3 = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    scaleAnimation.values = @[value1,value2,value3];
    scaleAnimation.keyTimes = @[@0,@0.7,@1];
    
    CABasicAnimation *baseAnimation = [CABasicAnimation animationWithKeyPath:@"hidden"];
    baseAnimation.toValue = [NSNumber numberWithBool:false];
    CAAnimationGroup *groupAnimation = [CAAnimationGroup animation];
    groupAnimation.animations = @[scaleAnimation,baseAnimation];
    groupAnimation.duration = 0.3;
    groupAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    groupAnimation.repeatCount = 1;
    
    self.layer.hidden = NO;
    [self.layer addAnimation:groupAnimation forKey:@"ani"];
}

- (void)hiddenPopoer{
    self.hidden = YES;
    [self removeFromSuperview];
}


- (void)selectButton:(UIButton*)btn{
    if ([self.delegate respondsToSelector:@selector(selecteItemIndex:buttonIndex:)]) {
        [self.delegate selecteItemIndex:self.index buttonIndex:btn.tag - 2000];
    }
    [self hiddenPopoer];
}



@end
